package fxification.java;

import com.sun.javafx.functions.Function0;
import com.sun.javafx.functions.Function1;

/**
 *
 * @author vvivien
 */
public class JavaFXTaskContext implements TaskRunnerContext{
    private Function0<? extends Object> workCallback;
    private Function0<Void> stopCallback;
    private Function1<Void, ? super Object> completedCallback;
    private Function1<Void, ? super Throwable> failureCallback;
    private Boolean stopped = false;
    
    private final javafx.async.Task fxTask;

    public JavaFXTaskContext(javafx.async.Task task) {
        fxTask = task;
    }

    @Override
    public Function0<Void> getStartTaskCallback() {
        synchronized(fxTask){
            return fxTask.get$onStart();
        }
    }

    @Override
    public void setStartTaskCallback(final Function0<Void> onStart) {
        synchronized(fxTask){
            fxTask.set$onStart(onStart);
        }
    }

    @Override
    public synchronized Function0<Void> getStopTaskCallback() {
        return stopCallback;
    }

    @Override
    public synchronized void setStopTaskCallback(final Function0<Void> onStopped) {
        stopCallback = onStopped;
    }

    @Override
    public Function0<Void> getTaskDoneCallback() {
        synchronized(fxTask){
            return fxTask.get$onDone();
        }
    }

    @Override
    public void setTaskDoneCallback(final Function0<Void> onDone) {
        synchronized(fxTask){
            fxTask.set$onDone(onDone);
        }
    }

    @Override
    public synchronized Function1<Void,? super Object> getTaskSucceededCallback() {
        return completedCallback;
    }

    @Override
    public synchronized void setTaskSucceededCallback(final Function1<Void, ? super Object> onSucceeded) {
        completedCallback = onSucceeded;
    }


    @Override
    public synchronized Function1<Void, ? super Throwable> getTaskFailedCallback() {
        return failureCallback;
    }

    @Override
    public synchronized void setTaskFailedCallback(final Function1<Void,? super Throwable> onFailure) {
        failureCallback = onFailure;
    }

    @Override
    public synchronized Function0<? extends Object> getTaskWork() {
        return workCallback;
    }

    @Override
    public synchronized void setTaskWork(final Function0<? extends Object> work) {
        workCallback = work;
    }

    @Override
    public Boolean isStarted() {
        synchronized(fxTask){
            return fxTask.get$started();
        }
    }

    @Override
    public Boolean isStopped() {
        synchronized(fxTask){
            return fxTask.get$stopped();
        }
    }

    @Override
    public Boolean isDone() {
        synchronized(fxTask){
            return fxTask.get$done();
        }
    }

    @Override
    public synchronized void setStarted(final Boolean flag) {
        synchronized(fxTask){
            fxTask.set$started(flag);
        }
    }

    @Override
    public void setStopped(final Boolean flag) {
        synchronized(fxTask){
            fxTask.set$stopped(true);
        }
    }

    @Override
    public void setDone(final Boolean flag) {
        synchronized(fxTask){
            fxTask.set$done(flag);
        }
    }

    @Override
    public Boolean isFailed() {
        synchronized(fxTask){
            return fxTask.get$failed();
        }
    }

    @Override
    public void setFailed(final Boolean flag){
        synchronized(fxTask){
            fxTask.set$failed(flag);
        }
    }

    @Override
    public void setFailureCause(Object obj) {
        synchronized(fxTask){
            fxTask.set$causeOfFailure(obj);
        }
    }

    @Override
    public Boolean isSuccessful() {
        synchronized(fxTask){
            return fxTask.get$succeeded();
        }
    }
    @Override
    public void setSuccessful(Boolean flag){
        synchronized(fxTask){
            fxTask.set$succeeded(flag);
        }
    }
}
